package com.ruoyi.workflow.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.workflow.domain.Message;
import com.ruoyi.workflow.dto.MessageResDto;
import com.ruoyi.workflow.dto.MessageSaveDto;
import com.ruoyi.workflow.mapper.UserMessageMapper;
import com.ruoyi.workflow.service.UserMessageService;
import io.swagger.models.auth.In;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author zhouzhixiong
 * @Date 2024/3/11 22:15
 */
@Service
public class UserMessageServiceImpl implements UserMessageService {

    private final UserMessageMapper messageMapper;

    @Autowired
    public UserMessageServiceImpl(UserMessageMapper messageMapper) {
        this.messageMapper= messageMapper;
    }

    @Override
    public void sendMessage(Message message) {
        MessageSaveDto messageSaveDto = new MessageSaveDto();
        BeanUtils.copyProperties(message, messageSaveDto,"messageType");
        messageSaveDto.setMessageType(message.getMessageType().getDescription());
        // 保存到数据库
        messageSaveDto.setCreateTime(new Date());
        messageSaveDto.setIsHandle(0);
        messageMapper.save(messageSaveDto);
    }

    @Override
    public AjaxResult<String> delete(List<Integer> messageIdList) {
        messageMapper.deleteBatch(messageIdList);
        return AjaxResult.success("删除成功");
    }

    @Override
    public AjaxResult<MessageResDto> detail(String messageId) {
        MessageResDto detail = messageMapper.detail(messageId);
        return AjaxResult.success(detail);
    }

    @Override
    public AjaxResult<String> handle(List<Integer> messageIdList) {
        Date date = new Date();
        messageMapper.handleBatch(messageIdList,date);
        return AjaxResult.success("操作成功");
    }

    @Override
    public List<MessageResDto> list(Long userId) {
        return messageMapper.list(userId);
    }
}
